﻿using AutoMapper;
using JIESHINet.IService;
using JIESHINet.Model.Vo;
using JIESHINet.Repository.Base;
using JIESHINet.Repository.Repository;
using System.Linq.Expressions;

namespace JIESHINet.Service
{
    public class BaseService<TEntity,TVO> : IBaseService<TEntity, TVO> where TEntity : class, new()
    {

        private readonly IMapper _mapper;
        private readonly IBaseRepository<TEntity> _baseReo;

        public BaseService(IMapper mapper, IBaseRepository<TEntity> baseReo)
        {
            _mapper = mapper;
            _baseReo = baseReo;
        }

        public async Task<long> Add(TEntity entity)
        {
            return await _baseReo.Add(entity);
        }

        public async Task<List<long>> AddSplit(TEntity entity)
        {
            return await _baseReo.AddSplit(entity);
        }

        public async Task<List<TVO>> Query()
        {
            var entites = await _baseReo.Query();
            //automapper转换
            var volist = _mapper.Map<List<TVO>>(entites);
            return volist;
        }

        public async Task<List<TVO>> QuerySplit(Expression<Func<TEntity, bool>> whereExpression, string orderByFields = null)
        {
            var entites = await _baseReo.QuerySplit(whereExpression, orderByFields);
            var volist = _mapper.Map<List<TVO>>(entites);
            return volist;
        }
    }
}